﻿namespace Cubezicles.UI
{
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    
    public class Button: Control
    {
        public int xpos, ypos;
        public string text;
        public SpriteFont font;

        public bool Active { get; set; }

        public void Enable()
        {
            Active = true;
        }
        public void Disable()
        {
            Active = false;
        }

        public Color btnColor = Color.White;

        int btnLeft = 15, btnMiddle = 30; //Width of the portions
        int txtLen, txtH;
        bool varSet = false;

        public Button()
        {
            Active = true;
        }

        private void setVars() {
            if (!varSet)
            {
                txtLen = (int)font.MeasureString(text).X; // null reference unhandled
                txtH = (int)font.MeasureString(text).Y;
                GetBoundBox = (c, w, h) =>
                            new Rectangle(xpos, ypos, txtLen + 30, Texture.Height / 3);
            }
        }

        

        public new void Update(Viewport viewport, MouseState ms, MouseState oldms)
        {
            setVars();
            // Update HasFocus and also check in case we need to fire a click
            // delegate
            if (HasFocus = Contains(viewport, new Point(ms.X, ms.Y)))
            {
                if (Active && ms.LeftButton == ButtonState.Pressed && (oldms.LeftButton == ButtonState.Released || notholdbtn))
                {
                    OnClick(this);
                }
            }
        }

        // Draw this icon/menu item
        public new void Draw(SpriteBatch spriteBatch, Viewport viewport)
        {
            setVars();
            GetBoundBox = (c, w, h) =>
                    new Rectangle(xpos, ypos, btnLeft * 2 + txtLen, Texture.Height / 3);
            Rectangle bound = GetBoundBox(this, viewport.Width, viewport.Height);

            //Fix Hovering.......... Sigh.
            int btnYdisplace = HasFocus ? Texture.Height / 3 : 0;
            if (!Active) { btnYdisplace = 2 * Texture.Height / 3; }

            //Draw the left end of the button
            spriteBatch.Draw(Texture,
                new Rectangle(xpos, ypos, btnLeft, Texture.Height / 3), //Destination
                new Rectangle(0, btnYdisplace, btnLeft, Texture.Height / 3), //Source
                btnColor);
            //Draw Middle Portion
            spriteBatch.Draw(Texture,
                new Rectangle(xpos + btnLeft, ypos, txtLen, Texture.Height / 3), //Destination
                new Rectangle(btnLeft, btnYdisplace, btnMiddle, Texture.Height / 3), //Source
                btnColor);
            //Draw String
            spriteBatch.DrawString(font, text, new Vector2(xpos + btnLeft, ypos + (((Texture.Height / 3) - txtH) / 2)), Color.White);
            //Draw Left Portion
            spriteBatch.Draw(Texture,
                new Rectangle(xpos + btnLeft + txtLen, ypos, btnLeft, Texture.Height / 3), //Destination
                new Rectangle(btnLeft + btnMiddle, btnYdisplace, btnLeft, Texture.Height / 3), //Source
                btnColor);
            
        }

        
    }
}
